package com.newtype.meeting.client.mvc.backend;

import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.newtype.meeting.client.AppEvents;
import com.newtype.meeting.client.GMeeting;
import com.newtype.meeting.client.model.Degree;
import com.newtype.meeting.client.model.MeetingCategory;
import com.newtype.meeting.client.model.Title;
import com.newtype.meeting.client.mvc.AppView;
import com.newtype.meeting.client.widget.backend.AccountManagementPanel;
import com.newtype.meeting.client.widget.backend.DepartmentAddWindow;
import com.newtype.meeting.client.widget.backend.DepartmentManagementPanel;
import com.newtype.meeting.client.widget.backend.MainCategoryWindow;
import com.newtype.meeting.client.widget.backend.MeetingManagementPanel;
import com.newtype.meeting.client.widget.backend.NextCategoryWindow;
import com.newtype.meeting.client.widget.backend.OpenedNewAccountPanel;
import com.newtype.meeting.client.widget.backend.PersonnelWindow;
import com.newtype.meeting.client.widget.backend.TitleEditWindow;
import com.newtype.meeting.client.widget.backend.TitleManagementPanel;
import com.newtype.meeting.client.widget.backend.VcardEditPanel;
import com.newtype.meeting.client.widget.shared.DeptTreeMultiSelectWindow;
import com.newtype.meeting.client.widget.shared.DeptTreeSingleSelectWindow;
import com.newtype.meeting.shared.AccountDTO;
import com.newtype.meeting.shared.AccountSummaryDTO;
import com.newtype.meeting.shared.DepartmentTreeDTO;

/**
 * 后台管理员操作视图类
 * @author 汪恭泽
 * Jun 7, 2011 1:35:09 PM
 */
public class BackendView extends View {
	private AccountManagementPanel accountManagementPanel;
	private DepartmentManagementPanel departmentManagementPanel;
	private MeetingManagementPanel meetingManagementPanel;
	private TitleManagementPanel titleManagementPanel;
	private DeptTreeSingleSelectWindow singleSelectWindow;
	private DeptTreeMultiSelectWindow multiSelectWindow;
	private DepartmentAddWindow departmentAddWindow;
	private OpenedNewAccountPanel openedNewAccountPanel;
	private PersonnelWindow personnelWindow;
	private TitleEditWindow titleEditWindow;
	private MainCategoryWindow mainCategoryWindow;
	private NextCategoryWindow nextCategoryWindow;
	private VcardEditPanel vcardEditPanel;//修改我的资料面板
	private LayoutContainer wrapper = Registry.get(AppView.CENTER_PANEL);//主面板

	public BackendView(Controller controller) {
		super(controller);
	}

	@Override
	protected void initialize() {
		super.initialize();
		accountManagementPanel = new AccountManagementPanel();
		meetingManagementPanel = new MeetingManagementPanel();
		titleManagementPanel = new TitleManagementPanel();
		departmentAddWindow = new DepartmentAddWindow();
		departmentManagementPanel = new DepartmentManagementPanel();
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.ViewAccountManagement) {
			wrapper.removeAll();
			wrapper.add(accountManagementPanel);
			wrapper.layout();
			return;
		} else if (event.getType() == AppEvents.ViewDepartmentManagement) {
			//接收参数
			DepartmentTreeDTO root = event.getData("root");
			wrapper.removeAll();
			TreeStore<ModelData> store = departmentManagementPanel.getStore();
			store.removeAll();
			store.add(root.getChildren(), true);
			wrapper.add(departmentManagementPanel);
			wrapper.layout();
			return;
		} else if (event.getType() == AppEvents.ViewMeetingManagement) {
			wrapper.removeAll();
			//接收参数
			MeetingCategory model = event.getData("root");

			TreeStore<ModelData> treeStore = meetingManagementPanel
					.getTreeStore();
			treeStore.removeAll();
			treeStore.add(model.getChildren(), true);
			wrapper.add(meetingManagementPanel);
			wrapper.layout();
			return;
		} else if (event.getType() == AppEvents.ViewTitleManagement) {
			wrapper.removeAll();
			wrapper.add(titleManagementPanel);
			wrapper.layout();

			GroupingStore<Title> store = titleManagementPanel.getStore();
			store.removeAll();
			store.add(event.<List<Title>> getData());
			wrapper.layout();
			return;
		} else if (event.getType() == AppEvents.ToDeptTreeSingleWindow) {
			Map<String, Field<String>> pDept = event.getData("pDept");
			DepartmentTreeDTO root = event.getData("root");
			singleSelectWindow = new DeptTreeSingleSelectWindow(pDept);
			TreeStore<ModelData> store = singleSelectWindow.getTreeStore();
			store.removeAll();
			store.add(root.getChildren(), true);
			singleSelectWindow.show();

		} else if (event.getType() == AppEvents.ToDeptTreeMultiWindow) {
			//接收参数
			Map<String, Field<String>> dept = event.getData("dept");
			DepartmentTreeDTO root = event.getData("root");
			multiSelectWindow = new DeptTreeMultiSelectWindow(dept);
			TreeStore<ModelData> store = multiSelectWindow.getTreeStore();
			store.removeAll();
			store.add(root.getChildren(), true);
			multiSelectWindow.show();
		} else if (event.getType() == AppEvents.ToDeptAddWindow) {
			departmentAddWindow.show();
		} else if (event.getType() == AppEvents.ToOpenedNewAccount) {
			wrapper.removeAll();
			AccountDTO account = event.getData("accountDTO");
			List<Degree> degrees = event.getData("degrees");
			List<Title> titles = event.getData("titles");
			openedNewAccountPanel = new OpenedNewAccountPanel(account, degrees,
					titles);
			wrapper.add(openedNewAccountPanel);
			wrapper.layout();
			return;
		} else if (event.getType() == AppEvents.ToAddNewAccountWindow) {
			List<Degree> degrees = event.getData("degrees");
			List<Title> titles = event.getData("titles");

			personnelWindow = new PersonnelWindow(new AccountDTO(), degrees,
					titles);
			personnelWindow.show();
		} else if (event.getType() == AppEvents.ToTitleEditWindow) {
			Title title = event.getData("titles");
			List<AccountSummaryDTO> accounts = event.getData("accounts");
			titleEditWindow = new TitleEditWindow(title);
			ListStore<AccountSummaryDTO> store = titleEditWindow.getStore();
			store.removeAll();
			store.add(accounts);
			titleEditWindow.show();
		} else if (event.getType() == AppEvents.ToMainCategoryWindow) {
			MeetingCategory category = event.getData("category");
			boolean flag = event.getData("flag");
			mainCategoryWindow = new MainCategoryWindow(category, flag);
			mainCategoryWindow.show();
		} else if (event.getType() == AppEvents.ToNextCategoryWindow) {
			//接收参数
			MeetingCategory category = event.getData("category");
			List<MeetingCategory> mainCates = event.getData("mainCates");
			MeetingCategory parentCate = event.getData("parentCate");
			boolean f1 = event.getData("f1");
			boolean f2 = event.getData("f2");
			boolean flag = false;
			if (f1 || f2) {
				flag = true;
			}
			nextCategoryWindow = new NextCategoryWindow(category, flag,
					mainCates, parentCate);
			ListStore<MeetingCategory> store = nextCategoryWindow.getStore();
			store.removeAll();
			store.add(mainCates);
			nextCategoryWindow.show();
		} else if (event.getType() == AppEvents.CheckAccountIDExist) {
			Boolean result = event.getData("result");
			Registry.register(GMeeting.CHECKEXIST, result);
		} else if (event.getType() == AppEvents.ToVcardEdit) {
			onToVcardEdit(event);
		}
	}

	/**
	 * 跳转到修改我的资料面板
	 * @param event
	 */
	private void onToVcardEdit(AppEvent event) {
		//接收参数
		AccountDTO accountDTO = event.getData("accountDTO");
		vcardEditPanel = new VcardEditPanel(accountDTO);
		wrapper.removeAll();
		wrapper.add(vcardEditPanel);
		wrapper.layout();
	}

}
